import { getQueryParams } from '@/utils/utils';
import type { Effect, Reducer } from 'umi';

export interface CommonState {
  companyList: RecordItem[];
  companyMessageA: RecordItem;
}

interface CommonModel {
  namespace: string;
  state: CommonState;
  effects: {
    getEnterpriseList: Effect;
  };
  reducers: {
    updateState: Reducer<CommonState>;
  };
}

const Common: CommonModel = {
  namespace: 'common',
  state: {
    companyList: [],
    companyMessageA: {},
  },
  effects: {
    *getEnterpriseList({ payload }, { call, put }) {
      const data: RecordItem = yield call(payload);
      yield put({
        type: 'updateState',
        payload: {
          companyList: data.map((item: RecordItem) => {
            return { ...item, key: item.id };
          }),
          companyMessageA: data.find((item: RecordItem) => item.code === getQueryParams().orgCode),
        },
      });
    },
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};

export default Common;
